package com.xmy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.study.entity.EduClassType;
import com.xmy.study.service.IEduClassTypeService;
import com.xmy.study.util.Code;
import com.xmy.study.util.IdWorker;
import com.xmy.study.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 班型 前端控制器
 * </p>
 *
 * @author Coly
 * @since 2023-06-20
 */
@RestController
@RequestMapping("/api/admin/classType")
public class ClassTypeController {
    @Autowired
    IdWorker idWorker;

    @Autowired
    IEduClassTypeService classTypeService;

    @PostMapping("newClassType")
    @ApiOperation("新增班型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序号", required = true, dataType = "String")
    })
    public Result saveClassType(@RequestBody Map reMap) {
        String name = (String) reMap.get("name");
        String sort = (String) reMap.get("sort");

        EduClassType classType = new EduClassType();
        classType.setId(idWorker.nextId());
        classType.setName(name);

        if (StringUtils.isNotBlank(sort)) {
            //判断是否为数字
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            Boolean sortB = pattern.matcher(sort).matches();
            if (!sortB) {
                return new Result(Code.SAVE_ERROR, Code.SAVE_ERROR_MSG, "请正确输入排序号，例如：1");
            }
            classType.setSort(Integer.valueOf(sort));
        }

        classType.setStatus(1);
        classTypeService.save(classType);
        return new Result(Code.OK, null, "班型信息新增成功~");
    }

    @GetMapping("getClassType")
    @ApiOperation("查询班型信息")
    @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "String")
    public Result searchClassType(@RequestParam Map reMap) {
        String name = (String) reMap.get("name");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        List<EduClassType> eduCourseList = classTypeService.list(queryWrapper);
        return new Result(Code.OK, eduCourseList, "班型信息查询成功~");
    }

    @DeleteMapping("delClassType")
    @ApiOperation("删除班型信息")
    @ApiImplicitParam(name = "id", value = "名称", required = true, dataType = "String")
    public Result removeClassType(@RequestParam("id") String id) {
        EduClassType eduClassType = classTypeService.getById(id);
        if (Objects.isNull(eduClassType)) {
            return new Result(Code.EXP_ERROR, Code.EXP_MSG, "该班型不存在");
        }
        classTypeService.removeById(id);
        return new Result(Code.OK, null, "班型信息删除成功~");
    }

    @PostMapping("updateClassType")
    @ApiOperation("修改班型信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "课程id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "String")
    })
    public Result updateClassType(@RequestBody Map reMap) {
        String id = (String) reMap.get("id");
        String name = (String) reMap.get("name");
        EduClassType classType = classTypeService.getById(id);
        if (Objects.isNull(classType)) {
            return new Result(Code.EXP_ERROR, Code.EXP_MSG, "该班型不存在");
        }
        if (Objects.nonNull(name)) {
            classType.setName(name);
        }
        classTypeService.updateById(classType);
        return new Result(Code.OK, null, "班型信息修改成功~");
    }

    @GetMapping
    public Result getOne() {
//        System.out.println("成功:" + this.adminId);
        return new Result(Code.OK, "", "成功");
    }

    @GetMapping("/classType")
    @ApiOperation("获取全部班型")
    public Result searchCategory() {
        List<EduClassType> eduClassTypes = classTypeService.list();
        return new Result(Code.GET_OK, eduClassTypes, Code.GET_OK_MSG);
    }
}
